package com.java110.mall.acct.payment.business;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.factory.CallApiServiceFactory;
import com.java110.core.factory.GenerateCodeFactory;
import com.java110.dto.payment.PaymentOrderDto;
import com.java110.dto.shop.ShopDto;
import com.java110.dto.storeOrder.ResultComputeStoreOrderPriceDto;
import com.java110.dto.storeOrder.StoreOrderDto;
import com.java110.dto.storeOrder.StoreOrderReturnPayDto;
import com.java110.intf.cart.IUnifiedOrderInnerServiceSMO;
import com.java110.intf.shop.IShopInnerServiceSMO;
import com.java110.mall.acct.payment.IPaymentBusiness;
import com.java110.utils.util.Assert;
import com.java110.utils.util.StringUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("buyGroupGoods")
public class BuyGroupGoodsBusiness implements IPaymentBusiness {

    @Autowired
    private IShopInnerServiceSMO shopInnerServiceSMOImpl;

    @Autowired
    private IUnifiedOrderInnerServiceSMO unifiedOrderInnerServiceSMOImpl;

    @Override
    public PaymentOrderDto unified(ICmdDataFlowContext context, JSONObject reqJson) {

        String appId = context.getReqHeaders().get("app-id");
        String userId = context.getReqHeaders().get("user-id");

        Assert.hasKeyAndValue(reqJson, "shopId", "未包含商铺信息");
        Assert.hasKeyAndValue(reqJson, "groupId", "未包含拼团ID");
        Assert.hasKeyAndValue(reqJson, "valueId", "未包含规格ID");
        Assert.hasKeyAndValue(reqJson, "productId", "未包含商品");
        Assert.hasKeyAndValue(reqJson, "addressId", "未包含地址");
        Assert.hasKeyAndValue(reqJson, "goodsNum", "未包含购买数量");
        reqJson.put("user-id", userId);//
        reqJson.put("app-id", appId);//
        /**
         * ?page=1&row=1&shopId=502022081626050001&cartType=3306
         * &productId=902022120101840004&valueId=912022120139430012&goodsNum=1&groupId=102022122018530001
         */

        //todo 查询商铺信息
        //计算服务费
        ShopDto shopDto = new ShopDto();
        shopDto.setShopId(reqJson.getString("shopId"));
        List<ShopDto> shopDtos = shopInnerServiceSMOImpl.queryShops(shopDto);

        Assert.listOnlyOne(shopDtos, "商铺不存在");
        shopDto = shopDtos.get(0);

        JSONObject paramIn = new JSONObject();
        paramIn.put("page", 1);
        paramIn.put("row", 1);
        paramIn.put("shopId", reqJson.getString("shopId"));
        paramIn.put("cartType", "3306");
        paramIn.put("productId", reqJson.getString("productId"));
        paramIn.put("valueId", reqJson.getString("valueId"));
        paramIn.put("goodsNum", reqJson.getString("goodsNum"));
        paramIn.put("groupId", reqJson.getString("groupId"));
        ResultComputeStoreOrderPriceDto resultComputeStoreOrderPriceDto =  unifiedOrderInnerServiceSMOImpl.computeGroupProductPrice(reqJson);

        String orderId = GenerateCodeFactory.getGeneratorId("11");
        String feeName = "购买商品";
        double money = resultComputeStoreOrderPriceDto.getPayPrice();

        PaymentOrderDto paymentOrderDto = new PaymentOrderDto();
        paymentOrderDto.setOrderId(orderId);
        paymentOrderDto.setMoney(money);
        paymentOrderDto.setName(feeName);

        if (StringUtil.isNumber(shopDto.getServiceFee()) && Double.parseDouble(shopDto.getServiceFee()) > 0) {
            paymentOrderDto.setProfitSharing(true);
        }

        reqJson.put("payAmount", money + "");
        reqJson.put("loginUserId", userId);
        return paymentOrderDto;
    }

    @Override
    public void notifyPayment(PaymentOrderDto paymentOrderDto, JSONObject reqJson) {
        reqJson.put("orderId", paymentOrderDto.getOrderId());
        reqJson.put("payOrderId", paymentOrderDto.getTransactionId());
        ResultVo resultVo =  unifiedOrderInnerServiceSMOImpl.unifiedGroupProductOrder(reqJson);

    }
}
